package com.xh.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 字符串工具类
 * @author: ZhangHua
 * @date: 20125-04-26
 */

public class StringUtils {

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 *            字符串内容
	 * @return true: 空；false：非空
	 */
	public static Boolean isNull(String str) {
		if (str == null) {
			return true;
		}

		if (str.trim().equals("")) {
			return true;
		}

		return false;
	}

	/**
	 * 判断字符串是否非空
	 * 
	 * @param str
	 *            字符串内容
	 * @return true: 非空；false：空
	 */
	public static Boolean isNotNull(String str) {
		if (isNull(str)) {
			return false;
		}

		return true;
	}

	/**
	 * 验证字符串是否邮箱
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {
		boolean flag = false;

		try {
			String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}

		return flag;
	}

	/**
	 * 去掉字符串的左右空格字符
	 * 
	 * @param str
	 *            字符串内容
	 * @return 无左右空格的字符串
	 */
	public static String trim(String str) {
		if (isNull(str)) {
			return "";
		}

		return str.trim();
	}

	/**
	 * 将list转换成只含map值的list
	 * 
	 * @param paramList
	 * @return
	 */
	public static List<Map<String, String>> toListForMap(List paramList) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		for (int i = 0; i < paramList.size(); i++) {
			Map map = (Map) paramList.get(i);
			Map<String, String> itemMap = new HashMap<String, String>();

			// 遍历map
			Iterator it = map.entrySet().iterator();

			while (it.hasNext()) {
				Map.Entry entry = (Map.Entry) it.next();
				String value = entry.getValue() == null ? "" : entry.getValue()
						.toString();
				itemMap.put(entry.getKey().toString(), value);
			}

			list.add(itemMap);
		}

		return list;
	}

	/**
	 * 将map转换成json
	 * 
	 * @param param
	 *            map
	 * @return
	 */
	public static JSONObject toJsonByMap(Map param) {
		String json = "{}";
		JSONObject jsonObj = JSONObject.fromObject(json);
		jsonObj.put("data", param);

		return jsonObj;
	}

	/**
	 * 拼凑成xml字符串
	 * 
	 * @param retCode
	 *            返回码
	 * @param msg
	 *            返回码对应信息
	 * @param elementList
	 *            元素信息
	 * @param totalCount
	 *            记录数
	 * @return xml字符串
	 */
	public static StringBuffer makeUpXmlString(String retCode, String msg,
			List<Map<String, String>> elementList, String totalCount) {
		StringBuffer sb = new StringBuffer();

		sb.append(StringConst.XML_HEAD);
		sb.append("\n");
		sb.append("<data>");
		sb.append("\n");
		sb.append("<retCode>");
		sb.append("<![CDATA[" + retCode + "]]>");
		sb.append("</retCode>");
		sb.append("\n");
		sb.append("<msg>");
		sb.append("<![CDATA[" + msg + "]]>");
		sb.append("</msg>");
		sb.append("\n");
		sb.append("<responseBody>");
		sb.append("\n");

		if (StringUtils.isNotNull(totalCount)) {
			sb.append("<count>");
			sb.append("<![CDATA[" + totalCount + "]]>");
			sb.append("</count>");
			sb.append("\n");
		}

		if (elementList != null && elementList.size() > 0) {
			sb.append("<List type='list'>");
			sb.append("\n");

			Iterator<Map<String, String>> it = elementList.iterator();

			while (it.hasNext()) {
				sb.append("<Element>");
				sb.append("\n");
				Map<String, String> eleMap = it.next();
				Set<String> eleKeys = eleMap.keySet();

				for (String ele : eleKeys) {
					sb.append("<" + ele + ">");
					sb.append(eleMap.get(ele));
					sb.append("</" + ele + ">");
					sb.append("\n");
				}

				sb.append("</Element>");
				sb.append("\n");
			}

			sb.append("</List>");
			sb.append("\n");
		} else if (elementList != null) {
			sb.append("<List type='list'>");
			sb.append("\n");
			sb.append("</List>");
			sb.append("\n");

		}

		sb.append("</responseBody>");
		sb.append("\n");
		sb.append("</data>");

		return sb;
	}

	/**
	 * 将request请求中的数据修改成json形式的String
	 * 
	 * @param appString
	 *            格式是id=1&name=jack
	 * @return
	 */
	public static String getRequestToJsonString(String appString){
		String[] appSplit=appString.split("&");
	    Map<String,Object> map=new HashMap<String,Object>();
		for (int i = 0; i < appSplit.length; i++) {
			if(isNotNull(appSplit[i])){
			 String[] keyVa=appSplit[i].split("=");
			 map.put(keyVa[0],keyVa[1]);
			}
		}
		return JSONObject.fromObject(map).toString();
	}
	

}
